
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

namespace Forums
{
	public class ForumBizRules : Syrinx.BaseBizRules
	{
		public IBizObjList getAllCompanyForumSetGroupsForParent(IApplication app, IRuleContext context)
		{
			FlexiMap m = new FlexiMap();
			m.put("objectTreeName", "Forum");
			m.put("startDepth", 0);
			m.put("endDepth", 1);
			m.put("entireTree", true);
			m.put("listDetailLevel", "basicDetails");
			//context.put("parentId", "");

			IResults rc = context.RuleGroup.execute(context, "getArticles", m, (IBizObj)null);
			IBizObjList list = rc.CallResults as IBizObjList;
			if(list != null)
				list.Insert(0, (IBizObj)app.appData.get("ArticleParentDropDown-Root"));

			return list;
		}
		public IBizObjList getUserCommentAppFunctions(IApplication app, IRuleContext context, string specifierId)
		{
			try
			{
				IBizObjList appFuns = app.bizObjMgr.createList("DynamicAppFunction");
				IBizObjList articleMetaTypes = app.appData.get("ArticleMetaTypes") as IBizObjList;
				if (articleMetaTypes != null)
				{
					foreach (IBizObj metatype in articleMetaTypes)
					{
						if ("Forum".CompareTo(metatype.get("id")) != 0 && "Comment".CompareTo(metatype.get("id")) != 0)
						{
							IBizObj appfun = appFuns.insertNew();
							appfun.put("id", new StringBuilder(100).Append("Article/").Append(metatype.get("id")).Append("UserComment").ToString());
							appfun.put("description", new StringBuilder(100).Append("User comments for ").Append(metatype.get("name")).ToString());
							appfun.put("specifierId", "UserCommentAppFunction");
						}
					}
				}
				return appFuns;
			}
			catch (Exception e)
			{
				throw new BaseException("Error executing getArticleGroupAppFunctions bizRule.", e);
			}
		}

        public void translateForumThread(IApplication app, IRuleContext context, Swaf.BGuid.Guid threadId)
        {
			FlexiMap m = new FlexiMap();
			m.put("objectTreeName", "Forum");
            m.put("parentId", threadId);
            m.put("includeSelf", true);
			m.put("entireTree", true);
			m.put("listDetailLevel", DetailLevel.idOnly);

			IResults rc = context.RuleGroup.execute(context, "getArticles", m, (IBizObj)null);
			IBizObjList list = rc.CallResults as IBizObjList;
            if (list != null)
            {
                foreach (IBizObj art in list)
                    context.RuleGroup.execute(context, "translateArticle", art.get("id"), (Swaf.BGuid.Guid)null);
            }
        }
	}
}
